package com.aidex.biz.uitls;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.nio.charset.StandardCharsets;

public class APISecureUtil {
    /**
     * 加密
     */
    public static String encodeRSA(String appSecret, String publicKey) {
        RSA rsa1 = SecureUtil.rsa(null, publicKey);
        return rsa1.encryptBase64(appSecret, StandardCharsets.UTF_8, KeyType.PublicKey);
    }

    /**
     * 解密
     *
     * @param data
     * @param publicKey
     * @return
     */
    public static String decryptRSA(String data, String publicKey) {
        RSA rsa = SecureUtil.rsa(null, publicKey);
        String strData = rsa.decryptStr(data, KeyType.PublicKey);
        System.out.println("解密后的数据: " + strData);
        return strData;
    }
}
